/**
 * 
 */
package com.sx.article.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.sx.article.biz.IArticleBiz;
import com.sx.article.biz.IAttachmentBiz;
import com.sx.article.entity.ArticleAndAttachmentEntity;
import com.sx.article.entity.ArticleEntity;
import com.sx.article.entity.AttachmentEntity;
import com.sx.core.BaseAction;
import com.sx.core.CoreAction;
import com.sx.core.MyUtils;
import com.sx.rbac.entity.SysUserEntity;
import com.sx.util.SystemConfigInfo;
import com.sx.util.date.DateUtil;


/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class ArticleAction extends CoreAction implements ModelDriven<ArticleEntity>{
	private Log log = LogFactory.getLog(this.getClass());
	private IArticleBiz articlebiz;
	private IAttachmentBiz attachmentbiz;
	private ArticleEntity article = new ArticleEntity();
	private File fileimg;
	private String fileimgFileName;
	public static final String ROOT = "back/attachment";
	public static final String ROOTPATH = "./../../../";
	/**
	 * 审核文章信息
	 **/
	public void approvalArticleInfo(){
		try {
			boolean flag = articlebiz.approvalArticleInfo(article);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("审核文章信息",e);
		}
	}
	/**
	 *在没有文章录入时上传附件信息
	 *上传后把附件信息放到session中 
	 **/
	public void uploadFileInfo(){
		try {
			List<AttachmentEntity> list = (List<AttachmentEntity>)getSession().getAttribute("attachment");
			if(list==null){
				list = new ArrayList<AttachmentEntity>();
			}
			AttachmentEntity attachment = super.getUpdateFileInfo(ROOT,this.getFileimgFileName());
			String uid = getRequest().getParameter("uid");
			attachment.setFuserid(uid);
			//上传成功后往附件表里面插入附件信息
			Integer id = attachmentbiz.addAttachment(attachment);
			attachment.setFid(id);
			list.add(attachment);
			getSession().setAttribute("attachment",list);
			outJsonString("{success:true}");
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("新增文章时上传文件失败",e);
			// TODO: handle exception
		}
	}
	/**
	 * 
	 **/
	public void downloadFile(){
		String realname = getRequest().getParameter("realname");
		String filename = getRequest().getParameter("filename");
		String path = getRequest().getParameter("path");
		path = getRealyPath(path.substring(3,path.length())+"\\"+filename);
		super.downLoadAttachment(realname, filename, path);
	}
	/**
	 *删除附件信息 
	 **/
	public void deleteArticleAttachments(){
		try {
			Integer aid = article.getAid();
			String fid = getRequest().getParameter("fid");
			Integer _fid = new Integer(fid);
			ArticleAndAttachmentEntity articleattachment = new ArticleAndAttachmentEntity(aid,_fid);
			boolean flag = this.attachmentbiz.deleteAttachmentByArticleId(articleattachment);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			// TODO: handle exception
			outJsonString("{success:false}");
		}
	}
	/**
	 *删除文章信息 
	 **/
	public void deleteInfo(){
		boolean flag = false;
		try {
			flag = articlebiz.deleteInfo(article);
			if (flag) {
				outJsonString("{success:true}");
			}else{
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("删除文章信息成功!",e);
		}
	}
	/**
	 * 编辑文章信息
	 **/
	public void editInfo(){
		try {
			List<AttachmentEntity> list = (List<AttachmentEntity>)getSession().getAttribute("attachment");
			article.setAttachment(list);
			boolean flag = this.articlebiz.editInfo(article);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("编辑文章信息成功!",e);
		}
	}
	/**
	 *按照id获取文章信息 
	 **/
	public void findArticleInfoById(){
		try {
			ArticleEntity articleinfo = articlebiz.findArticleById(article);
			String str = JSONObject.toJSONString(articleinfo);
			outJsonString("{success:true,data:"+str+"}");
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("获取文章信息失败",e);
			// TODO: handle exception
		}
	}
	/**
	 * 查询我的表单信息可以进行按照标题获文章内容进行分页查询
	 **/
	public void findArticleInfo(){
		String str = "{totalCount:0,data:[]}";
		try {
			System.out.println(article.getKeyword());
			str = articlebiz.findArticleInfo(article);
			outJsonString(str);
		} catch (Exception e) {
			outJsonString(str);
			// TODO: handle exception
			log.error("查询我的表单信息并",e);
		}
	}
	
	
	/**
	 *上传表单信息
	 **/
	public void uploadAttachments(){
		try {
			String _uploadpath = getRequest().getParameter("path");
			
			String path = "";
			if (_uploadpath==null||("").equals(_uploadpath)) {
				path = "workflow";
			} else {
				path = _uploadpath;
			}
			//文件上传
			//获取系统在计算机中的物理路径 如 C：\ncfams\webroot
			//E:\MyProject\PracticeProject\lsh\WebRoot\back//attachmentworkflow\2011-09-05day\
			String rootPath = getSession().getServletContext().getRealPath("/"); 
			rootPath += ROOT+"//"+path+"//";  
		    //根据配置的标识来创建日期文件夹 重属性文件中读取叫 dateflag 的属性
			String day = SystemConfigInfo.ARTICLE_FILE_FOLDER_CREATETIME;
		   /* log.error("day========"+day);*/
			String sp = rootPath + DateUtil.getDateName(day)+day+"//";
		    //获取网页中的web路径
			String webcontentpath = ROOTPATH+"back/attachment/"+path+"/"+DateUtil.getDateName(day)+day;
			//根据给定的名称 ”sp“ 创建文件夹
			MyUtils.mkDirectory(sp);
			String _filename = this.getFileimgFileName();
			//开始进行上传的操作
	        String filename = MyUtils.upload(this.getFileimgFileName().replaceAll(" ", ""), sp, this.getFileimg());  
	        //上传完成后获取文件网站全目录信息
	        String wholefilename = webcontentpath+"/"+filename;
		} catch (Exception e) {
			log.error("上传表单附件信息",e);
		}
		outJsonString("{success:true}");
	}
	/**
	 *新增信息 
	 **/
	public void addInfo(){
		try {
			SysUserEntity _user = super.getUserFromSession();
			List<AttachmentEntity> list = (List<AttachmentEntity>)getSession().getAttribute("attachment");
			article.setAttachment(list);
			Integer id = articlebiz.addInfo(article);
			if (id!=null) {
				outJsonString("{success:true,id:"+id.intValue()+"}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			outJsonString("success:false");
			log.error("新增文章信息出错请查明原因",e);
			// TODO: handle exception
		}
	}
	/**
	 * @return the articlebiz
	 */
	public IArticleBiz getArticlebiz() {
		return articlebiz;
	}

	/**
	 * @param articlebiz the articlebiz to set
	 */
	public void setArticlebiz(IArticleBiz articlebiz) {
		this.articlebiz = articlebiz;
	}
	public ArticleEntity getModel() {
		// TODO Auto-generated method stub
		return article;
	}
	/**
	 * @return the fileimg
	 */
	public File getFileimg() {
		return fileimg;
	}
	/**
	 * @param fileimg the fileimg to set
	 */
	public void setFileimg(File fileimg) {
		this.fileimg = fileimg;
	}
	/**
	 * @return the fileimgFileName
	 */
	public String getFileimgFileName() {
		return fileimgFileName;
	}
	/**
	 * @param fileimgFileName the fileimgFileName to set
	 */
	public void setFileimgFileName(String fileimgFileName) {
		this.fileimgFileName = fileimgFileName;
	}
	/**
	 * @return the attachmentbiz
	 */
	public IAttachmentBiz getAttachmentbiz() {
		return attachmentbiz;
	}
	/**
	 * @param attachmentbiz the attachmentbiz to set
	 */
	public void setAttachmentbiz(IAttachmentBiz attachmentbiz) {
		this.attachmentbiz = attachmentbiz;
	}
	
}
